/*****************************************************************
**                                                             	**
**  Copyright (C) 2004 Amlogic,Inc.                            	**
**  All rights reserved                                        	**
**        Filename : DeviceSelect.c /Project:AVOS		       **
**        Revision : 1.1                                       	**
**                                                             	**
*****************************************************************/
#include "../menu.h"
#include "DeviceSelectEvent.h"
#include "NetmenuSetupEvent.h"

INT32S WinDeviceSelectMsgProc(PHWND hwnd, INT32U msg_type, INT32S lpParam)
{
    switch( msg_type){
        case WM_SHOWWINDOW:
            return WinDeviceSelectOnShow(hwnd, lpParam) ;
    }
    return 0 ;
}

void SetWinDeviceSelectPalette(void)
{
    YUVENTRY mask_para[4] = {0,0,0,4095};
    AWSetAlphaMask(0,4, mask_para);
    YUVENTRY palette_para[16] = {0x8000 | 16392,0x8000 | 21176,0x8000 | 17752,0x8000 | 16392,17592,19496,19832,19656,19992,18104,17673,17817,15864,15721,15912,16344};
    AWSetPalette(0,16, palette_para);
    return ;
}

void SetWinDeviceSelectPos(HWND* pControl, INT8U CurOsdSize)
{
    switch(CurOsdSize){
	case OsdSize576i:
	case OsdSize576p:
	case OsdSize480i:
	case OsdSize480p:
	case OsdSize480iCVBS:
	case OsdSize576iCVBS:
    #ifdef SUPPORT_VGA
	case OsdSizeVga:
    #endif
            RECT NormalWindowRect = {0,0,720,576};
            pControl->winrect = NormalWindowRect;
            CREATEWINDOWPARAM NormalWindowPara = {1,{{0,0,720,576},    {0,0,0,0},{0,0,0,0},{0,0,0,0}},{PF_PALETTE_256,0,0,0},    SetWinDeviceSelectPalette};
            AWSendMessage(pControl, WM_WINDOW_RESET_DRAW_AREA, (INT32S)&NormalWindowPara);
            break ;
    }//end of switch
    return;
}

HWND *CreateControlWinDeviceSelect(HWND* pParent,INT8U CurOsdSize)
{
    HWND* pNewControl;
    switch(CurOsdSize){
	case OsdSize576i:
	case OsdSize576p:
	case OsdSize480i:
	case OsdSize480p:
	case OsdSize480iCVBS:
	case OsdSize576iCVBS:
    #ifdef SUPPORT_VGA
	case OsdSizeVga:
    #endif
            CREATEWINDOWPARAM Normalwindow_para = {1,{{0,0,720,576},{0,0,0,0},{0,0,0,0},{0,0,0,0}},{PF_PALETTE_256,0,0,0},SetWinDeviceSelectPalette};
            //Create Menu Window -- 1
            pNewControl = AWCreateWinControl("TWindow","WinDeviceSelect",WS_OVERLAPPEDWINDOW,
		                      0,0,720,576,NULL, (INT32S)&Normalwindow_para,WinDeviceSelectMsgProc) ;
            pNewControl->win_id = 0 ;
            break ;
    }//end of switch
            return pNewControl;
}

void SetWinDeviceSelectInfoPos(HWND* pControl, INT8U CurOsdSize)
{
    int iLoop;
    switch(CurOsdSize){
	case OsdSize576i:
	case OsdSize576p:
	case OsdSize576iCVBS:
            pControl->winrect.top = pControl->parent->winrect.top + 48;
            pControl->winrect.left = pControl->parent->winrect.left + 0;
            pControl->winrect.width = 720;
            pControl->winrect.height = 480;
            pControl->draw_info_num = 8;
            RECT DrawRect576i[8] = {
            {pControl->winrect.left + 140,pControl->winrect.top + 218,110,118},
            {pControl->winrect.left + 250,pControl->winrect.top + 218,110,118},
            {pControl->winrect.left + 360,pControl->winrect.top + 218,110,118},
            {pControl->winrect.left + 470,pControl->winrect.top + 218,110,118},
	     {pControl->winrect.left + 140,pControl->winrect.top + 368,110,30},
	     {pControl->winrect.left + 150,pControl->winrect.top + 306,110,30},
	     {pControl->winrect.left + 100,pControl->winrect.top + 280,30,30},
	     {pControl->winrect.left + 590,pControl->winrect.top + 280,30,30},			
            };
            for(iLoop=0; iLoop < 8; iLoop++)
                memcpy(&(pControl->draw_info[iLoop]), &DrawRect576i[iLoop], sizeof(RECT));
            break ;
		
	case OsdSize480i:
	case OsdSize480p:
	case OsdSize480iCVBS:
    #ifdef SUPPORT_VGA
	case OsdSizeVga:
    #endif
            pControl->winrect.top = pControl->parent->winrect.top + 0;
            pControl->winrect.left = pControl->parent->winrect.left + 0;
            pControl->winrect.width = 720;
            pControl->winrect.height = 480;
            pControl->draw_info_num = 8;
            RECT NormalDrawRect[8] = {
            {pControl->winrect.left + 140,pControl->winrect.top + 218,110,118},
            {pControl->winrect.left + 250,pControl->winrect.top + 218,110,118},
            {pControl->winrect.left + 360,pControl->winrect.top + 218,110,118},
            {pControl->winrect.left + 470,pControl->winrect.top + 218,110,118},
	     {pControl->winrect.left + 140,pControl->winrect.top + 338,110,30},
	     {pControl->winrect.left + 150,pControl->winrect.top + 306,110,30},
	     {pControl->winrect.left + 90,pControl->winrect.top + 250,30,30},
	     {pControl->winrect.left + 600,pControl->winrect.top + 250,30,30},			
            };
            for(iLoop=0; iLoop < 8; iLoop++)
                memcpy(&(pControl->draw_info[iLoop]), &NormalDrawRect[iLoop], sizeof(RECT));
            break ;
    }//end of switch
    return;
}

INT32S WinDeviceSelectInfoMsgProc(PHWND hwnd, INT32U msg_type, INT32S lpParam)
{
    switch( msg_type){
        case WM_SHOWWINDOW:
            return WinDeviceSelectInfoOnShow(hwnd, lpParam) ;
        case WM_KEYDOWN:
            return WinDeviceSelectInfoOnKeyPress(hwnd, lpParam) ;
	 case WM_PAINT:
            return WinDeviceSelectInfoOnPaint(hwnd, lpParam) ;   
    }
    return 0 ;
}

HWND *CreateControlWinDeviceSelectInfo(HWND *pParent,INT8U CurOsdSize)
{
    //Create Control -- WinDeviceSelectInfo
    HWND *pNewControl;
    switch(CurOsdSize){
	case OsdSize576i:
	case OsdSize576p:
	case OsdSize576iCVBS:
	case OsdSize480i:
	case OsdSize480p:
	case OsdSize480iCVBS:
    #ifdef SUPPORT_VGA
	case OsdSizeVga:
    #endif
            pNewControl = AWCreateWinControl("TControl","WinDeviceSelectInfo",(WS_CHILDWINDOW | WS_CHILDWINDOW),
		                      0,0,720,480,pParent,0,WinDeviceSelectInfoMsgProc) ;
            pNewControl->win_id = 1 ;
            pNewControl->draw_info_num = 8;
            pNewControl->draw_info = (PDRAWINFO)AVMem_umalloc(sizeof(DRAWINFO) * pNewControl->draw_info_num) ;
            DRAWINFO NormalDrawDatas[8] = {
                          {0,0,0,0,(INT32U)0,DT_RLE_BMP_DATA},
                          {0,0,0,0,(INT32U)0,DT_RLE_BMP_DATA},
                          {0,0,0,0,(INT32U)0,DT_RLE_BMP_DATA},
                          {0,0,0,0,(INT32U)0,DT_RLE_BMP_DATA},
                          {0,0,0,0,(INT32U)0,DT_RLE_BMP_DATA},//ICON_DEVICENAME_BCK,DT_RLE_BMP_DATA},
                          {0,0,0,0,(INT32U)0,DT_RESOURCE_STRING_DATA},
                          {0,0,0,0,(INT32U)0,DT_RLE_BMP_DATA},
                          {0,0,0,0,(INT32U)0,DT_RLE_BMP_DATA},
                          };
            memcpy(pNewControl->draw_info,NormalDrawDatas, sizeof(DRAWINFO) * pNewControl->draw_info_num) ;
            break ;
    }//end of switch
            return pNewControl;
}

HWND* CreateMenuWinDeviceSelect(void)
{
    HWND *pNewControl ;
    TWINDOWS *pWin;
    HWND *pWinDeviceSelect = CreateControlWinDeviceSelect(NULL,CurrentOsdSize);
    SetWinDeviceSelectPos(pWinDeviceSelect,CurrentOsdSize);

    pNewControl= CreateControlWinDeviceSelectInfo(pWinDeviceSelect,CurrentOsdSize);
    SetWinDeviceSelectInfoPos(pNewControl,CurrentOsdSize);

    pWin = (TWINDOWS *)pWinDeviceSelect;
    pWin->pFocusControl = AWFindWinCtrlByName("WinDeviceSelectInfo");
    return pWinDeviceSelect;
}

void WinResetWinDeviceSelectControlsPos(HWND *pControl, INT8U CurOsdSize)
{
    switch(pControl->win_id){
        case 0://WinDeviceSelect
            SetWinDeviceSelectPos(pControl,CurOsdSize) ;
            break ;
        case 1://WinDeviceSelectInfo
            SetWinDeviceSelectInfoPos(pControl,CurOsdSize) ;
            break ;
    }//end of switch
}
void WinResetWinDeviceSelectPos(HWND *pControl, INT8U CurOsdSize)
{
    HWND *pChildControl;
    if(pControl == NULL)
        pControl = AWFindWinCtrlByName("WinDeviceSelect");
    if(pControl == NULL) return ;

    WinResetWinDeviceSelectControlsPos(pControl, CurOsdSize) ;
    pChildControl = pControl->children ;
    while(pChildControl != NULL){
    	WinResetWinDeviceSelectPos(pChildControl, CurOsdSize) ;
    	pChildControl = pChildControl->siblings ;
	}
	return ;
}

